package com.qf.controller;

import com.qf.pojo.PageBean;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:zhangyang
 * @Date: ${Date} 14:03
 * @Version 1.0
 */
@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //访问地址:http://localhost:8080/web工程/findUserByPageServlet?currentPage=xx&pageSize=xx
        //http://localhost:8080/web工程/findUserByPage  ----当用户直接点击查询列表--->展示分页的数据

        //1)接收参数
        String currentPage = request.getParameter("currentPage"); //当前页码
        String pageSize = request.getParameter("pageSize");//每页显示的条数

        //2)判断
        if(currentPage==null || "".equals(currentPage)){
            //默认值
            currentPage = "1" ;
        }

        if(pageSize==null || "".equals(pageSize)){
            pageSize ="3";
        }


        //3)调用service分页查询用户的数据
        UserService userService = new UserServiceImpl() ;
        PageBean<User> pb =  userService.findUserByPage(currentPage,pageSize) ;

        //4)通过业务层调用---获取PageBean
        if(pb!=null){
            //5)将PageBean对象存储到request域中
            request.setAttribute("pb",pb);

            //6)请求转发到list.jsp
            request.getRequestDispatcher("list.jsp").forward(request,response);
        }




    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

}
